﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    public class RoleManage : MarshalByRefObject
    {
        private static readonly RoleDal Dal = new RoleDal();


        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="model"></param>
        /// <param name="rightIDs"></param>
        /// <returns></returns>
        [Logging("添加角色：{@p0.RoleName}"), ExceptionCall]
        public Result Add(Role model, string rightIDs)
        {
            Result res = new Result();
            int roleId = Dal.Add(model, rightIDs);
            new RightsManage().DistributeForRole(roleId);

            return res;
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="model"></param>
        /// <param name="rightIDs"></param>
        [Logging("修改角色：{@p0.RoleName}"), ExceptionCall]
        public Result Edit(Role model, string rightIDs)
        {
            Result res = new Result();
            if (model.Remark == null)
            {
                model.Remark = string.Empty;
            }
            Dal.Edit(model, rightIDs);
            return res;
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="isAdmin"></param>
        [Logging("删除角色，Id：{@p0}"), ExceptionCall]
        public Result Delete(int roleId, bool isAdmin)
        {
            Result res = new Result();
            Dal.Delete(roleId);
            return res;
        }

        /// <summary>
        /// 加载角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Role GetInfo(int id)
        {
            return Dal.GetInfo(id);
        }

        /// <summary>
        /// 显示当前用户没有的角色
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="isAdmin"></param>
        /// <returns></returns>
        public IList<Role> GetListNotIn(int userId, bool isAdmin)
        {
            return Dal.GetListNotIn(userId, isAdmin);
        }

        /// <summary>
        /// 显示当前用户含有的角色
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="isAdmin"></param>
        /// <returns></returns>
        public IList<Role> GetListIn(int userId, bool isAdmin)
        {
            return Dal.GetListIn(userId, isAdmin);
        }

        /// <summary>
        /// 取消某个用户的某个角色
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="roleId"></param>
        [Logging("取消角色"), ExceptionCall]
        public Result TakeIt(int userId, int roleId)
        {
            Result res = new Result();
            Dal.TakeIt(userId, roleId);
            new RightsManage().Distribute(userId);

            return res;
        }

        /// <summary>
        /// 授予某个用户的某个角色
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="roleId"></param>
        [Logging("授予角色"), ExceptionCall]
        public Result AuthorizeIt(int userId, int roleId)
        {
            Result res = new Result();
            Dal.AuthorizeIt(userId, roleId);
            new RightsManage().Distribute(userId);
            return res;
        }

        /// <summary>
        /// 绑定角色列表
        /// </summary>
        /// <param name="isAdmin"></param>
        /// <returns></returns>
        public IList<Role> BindList(bool isAdmin)
        {
            return Dal.BindList(isAdmin);
        }
    }
}